<?php
// +----------------------------------------------------------------------
// | VueAdmin框架[基于ThinkPHP5开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2016-2018 http://www.vueadmin.org
// +----------------------------------------------------------------------
// | VueAdmin承诺基础框架永久免费开源，您可用于学习和商用，但必须保留软件版权信息。
// +----------------------------------------------------------------------
// | Author: sept <917088650@qq.com>，开发者QQ群：63131383
// +----------------------------------------------------------------------

namespace app\system\controller;


use app\lib\exception\ApiException;
use app\lib\requests\Response;
use app\lib\returnCode\Code;
use app\lib\returnCode\Msg;
use app\system\model\AdminMenu as MenuModel;
use app\system\validate\IDMustBePostiveInt;
use app\system\validate\MenuNew;

class Menu extends Base
{
    /**
     * 菜单列表
     * @author sept <917088650@qq.com>
     * @return mixed
     */
    public function view()
    {
        $fields = $this->request->get('fields','*');
        $id = $this->request->get('id',0);

        if (is_numeric($id)) {
            $data_info = MenuModel::getAllChild($id, 0,$fields);
        }

        if (!isset($data_info) || !$data_info) {
            throw new ApiException(Code::NO_FIND_RELATE_RECORD,Msg::NO_FIND_RELATE_RECORD);
        }

        return Response::success($data_info);
    }

    /**
     * 添加菜单
     * @author sept <917088650@qq.com>
     * @return mixed
     */
    public function add()
    {
        (new MenuNew())->goCheck();

        $data = $this->request->only('pid,title,url,sort,menu_type,status');

        $model = new MenuModel();
        $model->storage($data);

        return Response::success();
    }

    /**
     * 修改菜单
     * @param int $id
     * @author sept <917088650@qq.com>
     * @return mixed
     */
    public function edit($id = 0)
    {

        (new IDMustBePostiveInt())->goCheck();
        (new MenuNew())->goCheck();

        $data = $this->request->only('pid,title,url,sort,menu_type,status');

        $model = new MenuModel();
        $model->storage($data, $id);

        return Response::success();
    }

    /**
     * 删除菜单
     * @author sept <917088650@qq.com>
     * @return mixed
     */
    public function delete($id = 0)
    {
        //TODO 无法获取逗号分隔的数据id
        (new IDMustBePostiveInt())->goCheck();

        $model = new MenuModel();
        $model->del($id);

        return Response::success();
    }

}
